#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
//just like bitmap
//range [0...k-1]
void count_sort(int unsort[],int sort[],int length,int k)
{
  int* range = (int*)calloc(k,sizeof(int));

  //set counter array range
  for(int i=0;i<length;++i) 
    range[unsort[i] ]++;

  //put in sorted
  for(int i=0,j=0;i<k;i++){//loop for range[]
    while(range[i]-- != 0) {
      sort[j] = i;
      j++;
    }
  }
}

int main()
{
  int unsort[10] = {3,4,3,1,2,6,5,2,8,0};
  int sort[10];
  count_sort(unsort,sort,10,9);

  copy(sort,sort+10,ostream_iterator<int>(cout," ") );
  cout<<endl;
  return 0;
}
  
  
